<?php

use yii\db\Migration;

class m000004_000004_create_card extends Migration
{
    public function safeUp()
    {
        $this->createTable('{{%card}}', [
            'id' => $this->primaryKey(),
            'api_id' => $this->integer()->notNull()->comment('接口编号'),
            'isp_id' => $this->integer()->notNull()->comment('运营商编号'),
            'uid' => $this->integer()->comment('用户编号'),
            'package_id' => $this->integer()->comment('套餐编号'),
            'iccid' => $this->string(32)->comment('集成电路卡识别码20位字符'),
            'msisdn' => $this->string(16)->comment('物联卡号码最长13位数字'),
            'imsi' => $this->string(16)->comment('国际移动用户识别码不超过15位'),
            'status' => $this->integer()->notNull()->comment('状态'),
            'on_off' => $this->integer()->notNull()->defaultValue(0)->comment('开关机'),
            'ip' => $this->string(32)->comment('IP地址'),
            'data' => $this->decimal(20, 4)->notNull()->defaultValue(0)->comment('数据用量（B）'),
            'sms' => $this->integer()->notNull()->defaultValue(0)->comment('短信用量'),
            'voice' => $this->integer()->notNull()->defaultValue(0)->comment('语音用量（分钟）'),
            'batch_no' => $this->string(32)->notNull()->comment('批次号'),
            'create_time' => $this->integer()->notNull()->comment('创建时间'),
            'refresh_time' => $this->integer()->comment('刷新时间'),
            'remark' => $this->text()->comment('备注'),
        ]);
        $this->createIndex('card_iccid_idx', '{{%card}}', ['iccid'], true);
        $this->createIndex('card_msisdn_idx', '{{%card}}', ['msisdn'], true);
        $this->createIndex('card_imsi_idx', '{{%card}}', ['imsi'], true);
        $this->createIndex('card_batch_no_idx', '{{%card}}', ['batch_no']);
        $this->createIndex('fk_card_api1_idx', '{{%card}}', ['api_id']);
        try {
            $this->addForeignKey('fk_card_api1', '{{%card}}', ['api_id'], '{{%api}}', ['id']);
        } catch (Exception $e) {
            Yii::error($e->getMessage());
        }
        $this->createIndex('fk_card_isp1_idx', '{{%card}}', ['isp_id']);
        try {
            $this->addForeignKey('fk_card_isp1', '{{%card}}', ['isp_id'], '{{%isp}}', ['id']);
        } catch (Exception $e) {
            Yii::error($e->getMessage());
        }
        $this->createIndex('fk_card_user1_idx', '{{%card}}', ['uid']);
        try {
            $this->addForeignKey('fk_card_user1', '{{%card}}', ['uid'], '{{%user}}', ['id']);
        } catch (Exception $e) {
            Yii::error($e->getMessage());
        }
        $this->createIndex('fk_card_package1_idx', '{{%card}}', ['package_id']);
        try {
            $this->addForeignKey('fk_card_package1', '{{%card}}', ['package_id'], '{{%package}}', ['id']);
        } catch (Exception $e) {
            Yii::error($e->getMessage());
        }
        $this->batchInsert('{{%key_map}}', ['t', 'k', 'v'], [
            ['card_status', 1, '可测试'],
            ['card_status', 2, '库存'],
            ['card_status', 3, '可激活'],
            ['card_status', 4, '已激活'],
            ['card_status', 5, '已停用'],
            ['card_status', 6, '已失效'],
        ]);
    }

    public function down()
    {
        $this->delete('{{%key_map}}', ['t' => 'card_status']);
        $this->dropTable('{{%card}}');
    }
}
